

//
//  LoginViewController.m
//  NEWS_W_Y_S
//
//  Created by lanou3g on 15/10/23.
//  Copyright © 2015年 lanou3g.jake. All rights reserved.
//

#import "LoginViewController.h"
#import "LoginView.h"
#import "RegisterViewController.h"
#import <AVOSCloud/AVOSCloud.h>
#import <AVOSCloudSNS.h>

@interface LoginViewController ()
@property (nonatomic,strong) LoginView *loginView;

//@property(nonatomic,retain)NSString *name;
//@property(nonatomic,retain)NSString *pass;

@end

@implementation LoginViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"返回" style:UIBarButtonItemStyleDone target:self action:@selector(back)];
    [self steupView];
}

-(void)back{
    
    [self dismissViewControllerAnimated:YES completion:nil];
}
-(void)steupView{
    self.loginView = [[LoginView alloc] initWithFrame:self.view.bounds];
    [self.loginView.loginButton addTarget:self action:@selector(loginBn:) forControlEvents:UIControlEventTouchUpInside];
    [self.loginView.registButton addTarget:self action:@selector(registerBn:) forControlEvents:UIControlEventTouchUpInside];
    [self.loginView.wxloginButton addTarget:self action:@selector(wbloginBn:) forControlEvents:UIControlEventTouchUpInside];
    [self.loginView.qqloginButton addTarget:self action:@selector(qqloginBn:) forControlEvents:UIControlEventTouchUpInside];
    self.loginView.loginButton.userInteractionEnabled = YES;
    [self.view addSubview:self.loginView];
}

-(void)loginBn:(UIButton *)button{
    
    if (_loginView.userTextField.text == nil || [_loginView.userTextField.text isEqualToString:@""]||[_loginView.userTextField.text isEqualToString:@" "]||[_loginView.userTextField.text isEqualToString:@"  "]||[_loginView.userTextField.text isEqualToString:@"   "]||[_loginView.userTextField.text isEqualToString:@"    "]||[_loginView.userTextField.text isEqualToString:@"     "]){
        UIAlertController *alertC = [UIAlertController alertControllerWithTitle:@"温馨提示" message:@"用户名不能为空" preferredStyle:UIAlertControllerStyleAlert];
        UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];
        UIAlertAction *defineAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDestructive handler:nil];
        [alertC addAction:cancelAction];
        [alertC addAction:defineAction];
        [self presentViewController:alertC animated:YES completion:nil];
        return;
        
    }else if (_loginView.passwordTextField.text == nil ||[_loginView.passwordTextField.text isEqualToString:@""]||[_loginView.passwordTextField.text isEqualToString:@" "]||[_loginView.passwordTextField.text isEqualToString:@"  "]||[_loginView.passwordTextField.text isEqualToString:@"   "]||[_loginView.passwordTextField.text isEqualToString:@"    "]||[_loginView.passwordTextField.text isEqualToString:@"     "]){
        UIAlertController *alertC = [UIAlertController alertControllerWithTitle:@"温馨提示" message:@"密码不能为空" preferredStyle:UIAlertControllerStyleAlert];
        UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];
        UIAlertAction *defineAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDestructive handler:nil];
        [alertC addAction:cancelAction];
        [alertC addAction:defineAction];
        [self presentViewController:alertC animated:YES completion:nil];
        return;
    }

 
    
    AVQuery *query = [AVQuery queryWithClassName:@"MyLoadPractice"];
    [query selectKeys:@[@"userName",@"passWord"]];
    NSArray *result = [query findObjects];
    for (NSDictionary *dic in result){
        NSString *userName = dic[@"userName"];
        NSString *passWord = dic[@"passWord"];
        if ([_loginView.userTextField.text isEqualToString:userName] && [_loginView.passwordTextField.text isEqualToString:passWord]) {
            UIAlertController *alertC = [UIAlertController alertControllerWithTitle:@"提示" message:@"登陆成功" preferredStyle:UIAlertControllerStyleAlert];
            UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];
            UIAlertAction *ensureAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDestructive handler:^(UIAlertAction * _Nonnull action) {
                [self dismissViewControllerAnimated:YES completion:nil];
            }];
            [alertC addAction:cancelAction];
            [alertC addAction:ensureAction];
            [self presentViewController:alertC animated:YES completion:nil];
        }
        

    }
    
    UIAlertController *alertC = [UIAlertController alertControllerWithTitle:@"提示" message:@"登陆失败" preferredStyle:UIAlertControllerStyleAlert];
    UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];
    UIAlertAction *ensureAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDestructive handler:^(UIAlertAction * _Nonnull action) {
        [self dismissViewControllerAnimated:YES completion:nil];
    }];
    [alertC addAction:cancelAction];
    [alertC addAction:ensureAction];
    [self presentViewController:alertC animated:YES completion:nil];

    
    
}


#define kAppKey             @"4031722665"
#define kAppSecret          @"c01b2134652bbabe634ad5218f5d0c84"
#define kAppRedirectURI     @"http://www.weibo.com"

-(void)wbloginBn:(UIButton *)button{
    
    [AVOSCloudSNS setupPlatform:AVOSCloudSNSSinaWeibo withAppKey:kAppKey andAppSecret:kAppSecret andRedirectURI:kAppRedirectURI];
    
    [AVOSCloudSNS loginWithCallback:^(id object, NSError *error) {
        
        [AVUser loginWithAuthData:object platform:AVOSCloudSNSPlatformWeiBo block:^(AVUser *user, NSError *error) {
            if (error) {
                // 登录失败，可能为网络问题或 authData 无效
//                [TLAlertHelper showErrorHUDWithText:@"登录失败"];
                NSLog(@"登录失败");
            } else {
                // 登录成功
//                [TLAlertHelper showSuccessHUDWithText:@"登陆成功"];
                NSLog(@"登录成功");
                dispatch_async(dispatch_get_main_queue(), ^{
                    [self dismissViewControllerAnimated:YES completion:^{
                        
                    }];
                });
                
            }
        }];
    } toPlatform:AVOSCloudSNSSinaWeibo];
    
}


-(void)qqloginBn:(UIButton *)button{
    
    [AVOSCloudSNS setupPlatform:AVOSCloudSNSQQ withAppKey:@"1105216397" andAppSecret:@"FKqHJtB0aHIt4rwb" andRedirectURI:@""];
    
    [AVOSCloudSNS loginWithCallback:^(id object, NSError *error) {
        
        [AVUser loginWithAuthData:object platform:AVOSCloudSNSPlatformQQ block:^(AVUser *user, NSError *error) {
            if (error) {
                // 登录失败，可能为网络问题或 authData 无效
                //                [TLAlertHelper showErrorHUDWithText:@"登录失败"];
                NSLog(@"登录失败");
            } else {
                // 登录成功
                //                [TLAlertHelper showSuccessHUDWithText:@"登陆成功"];
                NSLog(@"登录成功");
                dispatch_async(dispatch_get_main_queue(), ^{
                    [self dismissViewControllerAnimated:YES completion:^{
                        
                    }];
                });
                
            }
        }];
    } toPlatform:AVOSCloudSNSQQ];
    
}


-(void)registerBn:(UIButton *)button{

    RegisterViewController *regist = [[RegisterViewController alloc] init];
    UINavigationController *uina = [[UINavigationController alloc] initWithRootViewController:regist];
    [self presentViewController:uina animated:YES completion:nil];
    
}

@end
